﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace debioscoopincdrie.Managers
{
    class ReservationManager
    {
        #region Fields
        private static ReservationManager INSTANCE;
        #endregion

        #region Constructors
        private ReservationManager()
        {
        }
        #endregion

        #region Static functions
        public static ReservationManager GetManager()
        {
            if (INSTANCE == null)
            {
                INSTANCE = new ReservationManager();
            }

            return INSTANCE;
        }
        #endregion

        #region Functions
        public void addReservation( bestelling order )
        {
            DatabaseManager.GetManager().GetEntities().bestelling.Add(order);
            saveChanges();
        }

        public List<bestelling> getReservations()
        {
            return DatabaseManager.GetManager().GetEntities().bestelling.ToList();
        }

        public bestelling getReservation(int reservationId)
        {
            return DatabaseManager.GetManager().GetEntities().bestelling.FirstOrDefault(b => b.bestelling_id == reservationId);
        }

        public void pickUpReservation(int reservationId)
        {
            bestelling reservation = DatabaseManager.GetManager().GetEntities().bestelling.FirstOrDefault(b => b.bestelling_id == reservationId);
            reservation.afgehaald = true;
            reservation.betaald = true;
            saveChanges();
        }

        public void cancelReservation(int reservationId)
        {
            bestelling reservation = DatabaseManager.GetManager().GetEntities().bestelling.FirstOrDefault(b => b.bestelling_id == reservationId);
            reservation.active = false;
            reservation.kaartje.Clear();
            saveChanges();
        }

        private void saveChanges()
        {
            try
            {
                DatabaseManager.GetManager().GetEntities().SaveChanges();
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine("Could not write database");
                System.Diagnostics.Debug.WriteLine(e.Message);
            }
        }
        #endregion
    }
}
